#include <points.hpp>
namespace ohe_framework
{

Point::Point()
{
  
}
Point::Point(unsigned short x_value,unsigned short y_value)
{
	x = x_value;
	y = y_value;
}
Point::~Point()
{

}


Blob::Blob()
{
    no_of_points=0;
}
Blob::~Blob()
{

}

int Blob::getNoOfPoints()
{
	return no_of_points;
}

void Blob::addPoint(Point point)
{
	points.push_back(point);
	no_of_points++;
}

bool Blob::addPoint(Point point,const int index)
{
    if(index<no_of_points)
	{
		points.insert(points.begin()+index,point);
	}
	else if(index==(no_of_points+1))
		{
			points.push_back(point);
		}
		else
		{
		  printErrorMessage("Index is invalid");
		  return false;
		}
	no_of_points++;
	return true;
}

bool Blob::removePoint(int index=-1)
{
	if((index==-1)&&(!points.empty()))
	{
		points.pop_back();
		no_of_points--;
		return true;
	}
	if(index<no_of_points)
	{
		points.erase(points.begin()+index);
		no_of_points--;
	}
	else
	{
		printErrorMessage("Index is invalid");
	    return false;
	}
	return true;
}

bool Blob::removePoint(Point& point)
{
	if(!points.empty())
	{
		for(int i=0;i<no_of_points;i++)
		{
			if((points[i].x==point.x)&&(points[i].y==point.y))
			{
				points.erase(points.begin()+i);
				no_of_points--;
				return true;
			}
		}
	}
	return false;
}

void Blob::getCenter(Point &center)
{
	unsigned long  x_center=0, y_center=0;
	for(int i=0;i<no_of_points;i++)
	{
		x_center+=points[i].x;
		y_center+=points[i].y;
	}
	center.x = (unsigned int)(x_center/no_of_points);
	center.y = (unsigned int)(y_center/no_of_points);
}

void Blob::getBoundingRectangle(Point& start,Point& end)
{
	unsigned int min_x,min_y,max_x,max_y;
	min_x=max_x=points[0].x;
	min_y=max_y=points[0].y;
	for(int i=1;i<no_of_points;i++)
	{
		if(points[i].x>max_x)
		{
			max_x = points[i].x;
		}
		if(points[i].x<min_x)
		{
			min_x = points[i].x;
		}
		if(points[i].y>max_y)
		{
			max_y = points[i].y;
		}
		if(points[i].y<min_y)
		{
			min_y = points[i].y;
		}
	}
        start.x = min_x;
        start.y = min_y;
        end.x = max_x;
        end.y = max_y;
}

std::vector<Point>* Blob::getPoints()
{
    return &points;
}

}
